package com.movisol.messenger.views;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

import com.movisol.messenger.R;
import com.movisol.messenger.controllers.MessengerController;
import com.movisol.messenger.views.controls.ContactConversationRow;
import com.movisol.messengercommunicator.data.Contact;
import com.movisol.messengercommunicator.data.Conversation;
import com.movisol.messengercommunicator.events.DataLoadedEvent;
import com.movisol.messengercommunicator.ilisteners.IConversationsListLoaded;
import com.movisol.messengerprofileservice.data.Friend;
import com.movisol.messengerprofileservice.helpers.MessengerProfileHelper;

public class Conversations extends Activity implements IConversationsListLoaded,OnClickListener{
	
	private final MessengerController mc = MessengerController.getInstance();
	private Context context;
	private LinearLayout ll;
//	private Button imBtnFriendList;
	private Button imBtnProfile;
	private Button imBtnChat;
	private Conversation conversation = null;
	private ContactConversationRow selectedConversationRow;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainpage);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		getWindow().addFlags(LayoutParams.FLAG_DITHER);

		mc.setCurrentPage(MessengerController.CONVERSATIONS_PAGE);
		context = mc.getContext();

		initializeGUI();
		mc.cc.addConversationListLoadedListener(this);

	}
	
	

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		refreshConversationList();
	}

	@Override
	protected void onStop() {
		super.onStop();

		if(selectedConversationRow != null)
			selectedConversationRow.setSelected(false);

		if (!isFinishing())
			finish();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onStop();
		mc.cc.removeConversationListLoadedListener(this);
	}



	private void initializeGUI() {
		
		ll = (LinearLayout) findViewById(R.id.layoutContactsMp);
				
		imBtnChat = (Button) findViewById(R.id.imBtnConversations);
		imBtnChat.setVisibility(View.GONE);
		
		imBtnProfile = (Button) findViewById(R.id.imBtnProfile);
		imBtnProfile.setVisibility(View.GONE);
		
	}

	@Override
	public void conversationListLoaded(DataLoadedEvent evt) {
		
		if(evt.getError() == null)
		{
			refreshConversationList();
		}
		
	}

	private void refreshConversationList() {
		
		// ll.removeAllViews();
		
		String userId;
		String nickName;
		
		if (mc.cc.getConversationsList() != null)
			for (Conversation conversation : mc.cc.getConversationsList().getConversations())
			{
				List<String> nickNameList = new ArrayList<String>();
				
				for (Contact contact : conversation.getContacts()) {
					userId = contact.getContactId();
					nickName = contact.getName();

					// If the contact had not set the user name, takes the name
					// from the Email
					if (nickName == null || nickName.trim().equals(""))
						nickName = contact.getEmail().substring(0, contact.getEmail().indexOf("@"));

					Friend friend = MessengerProfileHelper.findContactInFriends(userId, mc.pc.getFriends());

					if (friend != null)
						nickName = friend.getUserProfile().getCalculatedNickName();

					nickNameList.add(nickName);
				}
				
				ContactConversationRow ccr = new ContactConversationRow(context, conversation, nickNameList);
				ccr.setOnClickListener(this);
				ll.addView(ccr);
			}
		else
			mc.loadConversationsList();
		
	}


	@Override
	public void onClick(View v) {

		if(v instanceof ContactConversationRow)
		{
			selectedConversationRow = (ContactConversationRow) v;
			selectedConversationRow.setSelected(true);
			
			//Creates a new conversation, and fill it with the selected conversation data
			conversation = new Conversation();
			conversation.setContacts(new ArrayList<Contact>());
			
			for(Contact contact : selectedConversationRow.getConversation().getContacts())
			{
				Contact contacto = new Contact();
				contacto.setContactId(contact.getContactId());
				//TODO pa ver si va
				contacto.setEmail(contact.getEmail());
				contacto.setName(contact.getCalculatedName());
				conversation.getContacts().add(contacto);
			}
			
			conversation.setConversationId(selectedConversationRow.getConversation().getConversationId());
			conversation.setLastMessage(selectedConversationRow.getConversation().getLastMessage());
			conversation.setUri(selectedConversationRow.getConversation().getUri());
			
			mc.setCurrentConversation(conversation);
			Intent chat = new Intent(this, Chat.class);
			startActivity(chat);
		}
		
	}

}
